<?php

/**
 * 特殊上班，下班时间
*/
apf_require_class("Bll_BLL");
apf_require_class("Dao_Attendance_SpecialDay");
apf_require_class("Const_SpecialDay");
class Bll_Attendance_SpecialDay extends BLL{
    /**
     * 获取一年的
     * @param int $year
     * @param int $status
     * @return array|NULL
     */
    public function get_special_days_on_year($year=2012,$status=0){
        $where = array();
        if($status){
            $where['status'] = $status;
        }
        $where['s_year'] = $year;
        return $this->get_default_dao()->find($where);
    }
    /**
     * 获取所有
     * @param unknown_type $status
     */
    public function get_all_special_days($status=0){
        $where = array();
        if($status){
            $where['status'] = $status;
        }
        return $this->get_default_dao()->find($where);
    }
    /**
     * 获取两年的
     * @param int $year1
     * @param int $year2
     * @param int $status
     */
    public function get_special_days_on_two_year($year1=2012,$year2=2013,$status=0){
        $where = array();
        if($status){
            $where['status'] = $status;
        }
        $where['s_year IN ('.$year1.','.$year2.') AND 1=?'] = 1;
        return $this->get_default_dao()->find($where);
    }
    /**
     * 获取一天的
     * @param int $day
     * @return array|NULL
     */
    public function get_special_day($day=20121024){
        $result = $this->get_default_dao()->find(array('s_date'=>$day));
        if($result){
            return $result[0];
        }
        return null;
    }
    /**
     * 分页日志
     */
    public function get_special_days_list($params,$pagesize){
        $where = array();
        $page = isset($params['p'])?(int)$params['p']:1;
        $page = empty($page) ? 1:$page;

        $order = "id DESC";
        $offset = ($page-1)*$pagesize;

        $result = $this->get_default_dao()->get_special_days_list($where,$order,$pagesize,$offset);
        return $result;
    }
    public function update_or_insert($data){
        if(empty($data)){
            return ;
        }
        if($data['is_work_day'] == Const_SpecialDay::NEED_WORK){//工作日
            $data['status'] = $data['is_work_day'];
            $data['start_time'] = intval(substr($data['start_time'], 0,2).substr($data['start_time'], 3,2));
            $data['end_time'] = intval(substr($data['end_time'], 0,2).substr($data['end_time'], 3,2));
        }else if($data['is_work_day'] == Const_SpecialDay::NO_NEED_WORK){//非工作日
            if($data['is_org_holiday'] == Const_SpecialDay::NO_NEED_WORK_HOLIDAY){//法定节假日
                $data['status'] = $data['is_org_holiday'];
            }else if($data['is_org_holiday'] == Const_SpecialDay::NO_NEED_WORK_COMPANY){//非法定
                $data['status'] = $data['is_org_holiday'];
            }
            $data['start_time'] = 0;
            $data['end_time'] = 0;
        }
        unset($data['is_work_day']);
        unset($data['is_org_holiday']);
        $temp = explode(",", $data['s_date']);
        foreach ($temp as $one){
            if($one){
                $data['s_year'] = intval(substr($one, 0,4));
                $data['s_date'] = date('Ymd',strtotime($one));
                $one = $this->get_special_day($data['s_date']);
                if(empty($one)){
                    $this->get_default_dao()->insert($data);
                }else{
                    $this->get_default_dao()->update($data,array('id'=>$one['id']));
                }
            }
        }
    }
    private function get_default_dao(){
        return $this->get_dao('Dao_Attendance_SpecialDay');
    }


    /**
     * 根据年份获取这一年所有的SpecialDay
     * @param int $year
     * @return array
     */
    public function get_sd_by_year($year){
        if( empty($year) ){
            return array();
        }
        //$sql = 'SELECT `s_date`,`status` FROM special_day WHERE s_year='.(int)$year.' ORDER BY `s_date` ASC';
        $field =array('s_date','status');
        $where = 's_year='.(int)$year.' ';
        $order = 's_date ASC';
        //$result = $this->get_default_dao()->find_by_sql($sql);
        $result = $this->get_default_dao()->find_short($where, $order, $limit = 0, $offset = 0, $field);
        if( empty($result) ){
            return array();
        }else{
            //将status字段转化为int
            foreach( $result as &$v ){
                $status= $v['status'];
                if($status==2 ||$status==3){
                    $status=3;
                }
                $v = array( $v['s_date']=>intval($status));
            }
            return $result;
        }
    }
}